package com.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbCategoryMapper;
import com.group.pojo.Common;
import com.group.pojo.TbCategory;
import com.group.pojo.TbCategoryExample;
import com.group.pojo.TbUser;
import com.group.service.CategoryService;
import com.group.service.CommonService;
import com.group.util.CommonUtil;

@Service
public class CategoryServicempl implements CategoryService {
	@Autowired
	private TbCategoryMapper categoryMapper;
	@Autowired
	private CommonService commonService;

	// 查询所有的设备类别信息
	@Override
	public List<TbCategory> query(TbCategory category) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryStatusEqualTo("1");
		return categoryMapper.selectByExample(example);
	}

	// 根据类别id查询类别名称
	@Override
	public String getCategoryNameById(String categoryId) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryStatusEqualTo("1").andIdEqualTo(categoryId);
		List<TbCategory> list = categoryMapper.selectByExample(example);
		return list.get(0).getCategoryName();
	}

	// 根据类别名称查询类别id
	@Override
	public String getCategoryIdByName(String categoryName) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryNameEqualTo(categoryName).andCategoryStatusEqualTo("1");
		List<TbCategory> list = categoryMapper.selectByExample(example);
		return list.get(0).getId();
	}

	// 验真类别名是否存在
	@Override
	public String CheckcategoryName(String categoryName) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryNameEqualTo(categoryName);
		int count = categoryMapper.countByExample(example);
		if (count == 0) {
			return "false";
		}
		return "true";
	}

	// 根据类别id查询类别名称
	@Override
	public String getParentCategoryNameById(String parentCategoryId) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andIdEqualTo(parentCategoryId);
		String parentCategoryName = categoryMapper.selectByExample(example).get(0).getCategoryName();
		return parentCategoryName;
	}

	// 添加新的类别
	@Override
	public Map<String, String> saveCategory(HttpServletRequest request, TbCategory category) {
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");

		// 设置创建人、创建时间
		category.setCreateUserId(loginUser.getId());
		category.setCreateTime(new Date());
		
		// 唯一编号id
		String nextId = commonService.getTbCurrentDateNextId(new Common("tb_category", CommonUtil.getCurrentDate("yyyyMMdd")));
		category.setId(nextId);
		
		// 设置父类别id
		category.setCategoryStatus("1");
		
		//设置级别
		String parentCategoryId = category.getParentCategoryId();
		if ("-1".equals(parentCategoryId)) {
			category.setCategoryLevel(1);
		} else {
			TbCategory parentCategory = categoryMapper.selectByPrimaryKey(parentCategoryId);
			category.setCategoryLevel(parentCategory.getCategoryLevel()+1);
		}
		// 提交到数据库
		int insert = categoryMapper.insertSelective(category);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}

	// 根据类别id查询类别的信息
	@Override
	public List<TbCategory> getCategoryById(String id) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andIdEqualTo(id);
		List<TbCategory> list = categoryMapper.selectByExample(example);
		//list.get(0).setParentCategoryId(this.getCategoryNameById(list.get(0).getParentCategoryId()));
		return list;
	}

	// 修改类别信息
	@Override
	public Map<String, String> updateSave(TbCategory category) {
		
		if(category.getParentCategoryId().equals("-1")){
			category.setParentCategoryId("-1");
			category.setCategoryLevel(1);
		}else{
			int m = this.getCategoryById(category.getParentCategoryId()).get(0).getCategoryLevel();
			category.setCategoryLevel(m+1);
			//category.setParentCategoryId(this.getCategoryIdByName(this.getParentCategoryNameById(category.getId())));
		}
		
		// 提交到数据库
		int insert = categoryMapper.updateByPrimaryKeySelective(category);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
    //删除类别信息
	@Override
	public Map<String, String> batchDelete(String categoryId) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryStatusEqualTo("1").andParentCategoryIdEqualTo(categoryId);
		List<TbCategory> list = categoryMapper.selectByExample(example);
		TbCategory category = new TbCategory();
		category.setCategoryStatus("0");
		category.setId(categoryId);
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		 int upda = categoryMapper.updateByPrimaryKeySelective(category);
		 if (upda != 1) {
				map.put("success", "false");
				return map;
			}
		
		for (TbCategory cate : list) {
			cate.setCategoryStatus("0");
			int update = categoryMapper.updateByPrimaryKeySelective(cate);
			if (update != 1) {
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}
     //检查类名是否存在
	@Override
	public String categoryCheckRepect(String categoryName) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryNameEqualTo(categoryName).andCategoryStatusEqualTo("1");
		int cunt = categoryMapper.countByExample(example);
		if (cunt!=0){
			return "false";
		}
		return "true";
	}
	//查询类别是否有子类别
	@Override
	public Map<String, String> isExistSubcategory(String parentCategoryId, String categoryStatus) {
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andParentCategoryIdEqualTo(parentCategoryId).andCategoryStatusEqualTo(categoryStatus);
		int cunte = categoryMapper.countByExample(example);
		Map<String, String> map = CommonUtil.getReturnMap("success", "false");
		 if (cunte != 0) {
				map.put("success", "true");
				return map;
			}
		return map;
	}
	//修改类别时查询类别是否存在
	@Override
	public String updateCategoryCheckRepect(String categoryName, String categoryId) {
		
		TbCategoryExample example = new TbCategoryExample();
		TbCategoryExample example1 = new TbCategoryExample();
		example.createCriteria().andCategoryNameEqualTo(categoryName).andCategoryStatusEqualTo("1");
		example1.createCriteria().andIdEqualTo(categoryId).andCategoryStatusEqualTo("1");
		int cunt = categoryMapper.countByExample(example);
		if (cunt!=0){
			List<TbCategory> list = categoryMapper.selectByExample(example);
			List<TbCategory> list1 = categoryMapper.selectByExample(example1);
			if(list.get(0).getCategoryName().equals(list1.get(0).getCategoryName())){
				return "true";
			}else{
				return "false";
			}
		}else{
			return "true";
		}
		
	}
	// 添加类别时查看父类别名是否存在
	@Override
	public String CheckParentCategoryName(String parentCategoryName) {
		if(parentCategoryName.equals("-1")){
			return"true";
		}
		TbCategoryExample example = new TbCategoryExample();
		example.createCriteria().andCategoryNameEqualTo(parentCategoryName).andCategoryStatusEqualTo("1");
		int count = categoryMapper.countByExample(example);
		if (count != 0) {
			return "true";
		}
		return "false";
	}

}
